<?php 

Class wxruleModel extends appModel
{
	private $option=array();
	private $loop_num=0;
	public $db;
	

	public function getWeixin()
	{
		$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='appid' OR op_key='appsecret' OR op_key='token' OR op_key='wxareply'OR op_key='wxdreply'";
		$res=$this->getAll($sql);
		$arr=array();
		
		$arr['appid']=$res[0]['op_value'];
		$arr['appsecret']=$res[1]['op_value'];
		$arr['token']=$res[2]['op_value'];
		$arr['wxdreply']=$res[3]['op_value'];
		$arr['wxareply']=$res[4]['op_value'];

		return  $arr;
	}
	public function saveWeixin($info)
	{
		foreach($info as $k=>$v)
		{
			if(!($v===''))
			{
	
	
				$sql="UPDATE ".$this->table('options')." SET op_value='$v' WHERE op_key='$k'";
	
				$res=$this->query($sql);
			}
		}
	
		return $res;
	}
	public function ordering($val,$id)
	{
		$result=array('error'=>0,'content'=>'');
		$sql="UPDATE ".$this->table('category')." SET ordering='$val' WHERE id='".$id."'";
		$res=$this->query($sql);
		if(!res)
		{
			$result['error']=1;
			$result['content']='';
		}
		return $result;
	}
   
   public function get_rule_info($id)
   {
   	$sql=" SELECT id,name,status,type FROM ".$this->table('rule')." WHERE id='".$id."'";
   	$info=$this->getRow($sql);
   	
   
   	
   	if($info['type']==1)
   	{
   		$sql=" SELECT content FROM ".$this->table('basic_reply')." WHERE rid='".$id."'";
   		$info['content']=$this->getOne($sql);
   	}
   	else 
   	{
   		$sql=" SELECT id ,title ,description ,thumb ,url  FROM ".$this->table('news_reply')." WHERE rid='".$id."'";
   		$result=$this->getAll($sql);
   		$info['pic_img']=$result;
   		
   	}
   	return $info;
   }
   
 
   public function del_rule($id)
   {
   
   	$result=array('error'=>0,'content'=>'');
   	if($id)
   	{
   		$sql="SELECT type FROM ".$this->table('rule')." WHERE id=".$id;
   		$type=$this->getOne($sql);
   		$sql="DELETE FROM ".$this->table('rule')." WHERE  id='".$id."'";
   		 
   		$res=$this->query($sql);
   	   if($type==1)
   	   {
   				
   					
   				$sql="DELETE FROM ".$this->table('basic_reply')." WHERE rid='".$id."'";
   		
   				$res2=$this->query($sql);
   				if(!$res||!$res2)
   				{
   					$result['error']=1;
   					$result['content']='删除失败';
   				}
   	   }
   	   else
   	   {
   	   	   $sql="DELETE FROM ".$this->table('news_reply')." WHERE rid='".$id."'";
   	   	 
   	     	$res2=$this->query($sql);
   	   	    if(!$res||!$res2)
   	   	  {
   	   		$result['error']=1;
   	   		$result['content']='删除失败';
   	   	  }
   	   }
   		
   	}
   	return $result;
   }
 
  
    public function insert_rule($info,$content)
	{
		$field='';
		$value='';
		$result=array('error'=>0,'content'=>'','rule_id'=>0);
			foreach($info as $k=>$v)
			{
				if($v==='')
				{
			
				}
				else 
				{

					$field.="$k,";
					
					
					$value.="'$v',";
				}
			}
			$field=substr($field,0,strlen($field)-1);
			$value=substr($value,0,strlen($value)-1);
			
			$sql="INSERT INTO ".$this->table('rule')." (".$field.") VALUES(".$value.")";
			if(!$res=$this->query($sql))
			{
				$result['error']=1;
				$result['content']=Lang('SQL_ERROR');
			}
			else 
			{//添加产品图片
				$rid=$this->insert_id();
				$result['rule_id']=$rid;
				if($info['type']==1)
				{

					$sql="INSERT INTO ".$this->table('basic_reply')." (rid,content) VALUES('$rid','$content')";
				
					if(!$res=$this->query($sql))
					{
						$result['error']=1;
						$result['content']=Lang('SQL_ERROR');
					}
				}
				else 
				{
				   if(is_array($content)&&count($content)>0)
			       {
					   foreach($content as $key=>$val)
					  {
						//$sql="INSERT INTO ".$this->table('news_reply')." (rid,title,description,thumb,url) VALUES('$rid','$val[title]','$val[description]','$val[thumb]','$val[url]')";
					  		$content[$key]['rid']=$rid;
					  }	

					  if(!$res=$this->insert($content,'news_reply'))
					  {
					  	$result['error']=1;
					  	$result['content']=Lang('SQL_ERROR');
					  }
				   }
				}
			}
		
	
		return $result;
		//$this->make_thumb('/Uploads/ad/1398673595657549402.jpg','200','200');
	}
    public function update_rule($info,$content,$id)
	{
		
		$result=array('error'=>0,'content'=>'');
		if($id)
		{
		
			foreach($info as $k=>$v)
			{
				
					$where.=" $k='$v',";
						
				
			}
			
			$where.="modifytime='".$_SERVER['REQUEST_TIME']."'";
		
			$sql="UPDATE ".$this->table('rule')." SET ".$where." WHERE id='".$id."'";
			
		    $res=$this->query($sql);
		    if(!$res)
		    {
		    	$result['error']=1;
		    	$result['content']=Lang('SQL_ERROR');
		    }
		    else 
		    {
		    	if(is_array($content))
		    	{
		    	   if(is_array($content)&&count($content)>0)
		    		{
		    			foreach($content as $key=>$val)
		    			{
		    				//$sql="INSERT INTO ".$this->table('news_reply')." (rid,title,description,thumb,url) VALUES('$rid','$val[title]','$val[description]','$val[thumb]','$val[url]')";
		    				$content[$key]['rid']=$id;
		    			}
		    		
		    			if(!$res=$this->insert($content,'news_reply'))
		    			{
		    				$result['error']=1;
		    				$result['content']=Lang('SQL_ERROR');
		    			}
		    		}
		    	}
		    	else 
		    	{
		    		$sql="UPDATE ".$this->table('basic_reply')." SET content='".$content."' WHERE rid='".$id."'";
		    		$res=$this->query($sql);
		    	}
		    }
		    
		 
		    
		}
		return $result;
	}
	
	public function get_rule_list($page='')
	{
		if($page)
		{
		$listPage=array('data'=>'','pageLink'=>'');
	
		$result=$this->show_page("SELECT id,name,type FROM ".$this->table('rule')." ORDER BY id DESC",$page);
		$listPage['pagelink']=$result['page'];
		$listPage['data']=$result['result'];
	
	
		return $listPage;
		}
		else 
		{
			$sql="SELECT name,id FROM ".$this->table('rule');
			return $this->getAll($sql);
		}	
			
	}
}